<template>
  <div 
    :class="['character-item', { selected: isSelected, favorited: character.favorited }]"
    @click="emit('select', character)"
  >
    <div class="character-avatar">
      <div class="avatar-bg" :style="{ background: characterColor }">
        <img 
          v-if="characterAvatarUrl" 
          :src="characterAvatarUrl" 
          :alt="character.name" 
          class="character-avatar-image"
          @error="handleImageError"
        />
        <div v-else class="character-emoji">{{ characterEmoji }}</div>
      </div>
      <div class="character-status" :class="characterStatus"></div>
    </div>
    <div class="character-info">
      <h4>{{ character.name }}</h4>
      <p class="character-role">{{ characterRole }}</p>
      <div class="character-tags">
        <span v-for="tag in characterTags" :key="tag" class="tag">{{ tag }}</span>
      </div>
    </div>
    <div class="character-actions">
      <el-button 
        type="text" 
        size="small" 
        @click.stop="emit('toggleFavorite', character.id)"
        :class="['favorite-btn', { 'is-favorited': character.favorited }]"
      >
        <el-icon><Star v-if="!character.favorited" /><StarFilled v-else /></el-icon>
      </el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { Star, StarFilled } from '@element-plus/icons-vue'
import type { Character, LegacyCharacter } from '../../types/novel'

interface Props {
  character: Character | LegacyCharacter
  isSelected: boolean
}

const props = defineProps<Props>()

// 计算属性用于兼容新旧数据结构
const characterColor = computed(() => {
  if ('color' in props.character) {
    return (props.character as LegacyCharacter).color
  }
  return '#667eea' // 默认颜色
})

const characterEmoji = computed(() => {
  if ('emoji' in props.character) {
    return (props.character as LegacyCharacter).emoji
  }
  return '👤' // 默认头像
})

const characterAvatarUrl = computed(() => {
  // 如果是新格式的角色数据并且有头像图片
  if ('avatar_image' in props.character && props.character.avatar_image) {
    return props.character.avatar_image
  }
  return null
})

const handleImageError = () => {
  console.warn('角色头像加载失败:', props.character.name)
}

const characterRole = computed(() => {
  if ('role' in props.character) {
    return (props.character as LegacyCharacter).role
  }
  const roleMap = {
    protagonist: '主角',
    supporting: '配角',
    antagonist: '反角',
    minor: '次要角色'
  }
  return roleMap[props.character.role_type] || '角色'
})

const characterTags = computed(() => {
  if ('tags' in props.character) {
    return (props.character as LegacyCharacter).tags || []
  }
  return [] // 新格式暂无tags
})

const characterStatus = computed(() => {
  if ('status' in props.character) {
    return (props.character as LegacyCharacter).status
  }
  return 'online' // 默认在线状态
})

const emit = defineEmits<{
  select: [character: Character | LegacyCharacter]
  toggleFavorite: [id: number]
}>()
</script>

<script lang="ts">
export default {
  name: 'CharacterItem'
}
</script>

<style scoped>
.character-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.2s ease;
  margin-bottom: 8px;
  border: 2px solid transparent;
  position: relative;
}

.character-item:hover {
  background: #f8fafc;
  border-color: #e5e7eb;
}

.character-item.selected {
  background: linear-gradient(135deg, #667eea20, #764ba220);
  border-color: #667eea;
}

.character-item.favorited::before {
  content: '⭐';
  position: absolute;
  top: 8px;
  right: 8px;
  font-size: 12px;
  z-index: 1;
}

.character-avatar {
  position: relative;
  flex-shrink: 0;
}

.avatar-bg {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

.character-emoji {
  font-size: 24px;
}

.character-avatar-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
}

.character-status {
  position: absolute;
  bottom: 2px;
  right: 2px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 2px solid white;
}

.character-status.online { background: #10b981; }
.character-status.busy { background: #f59e0b; }
.character-status.offline { background: #6b7280; }

.character-info {
  flex: 1;
  min-width: 0;
}

.character-info h4 {
  font-size: 16px;
  font-weight: 700;
  color: #1f2937;
  margin-bottom: 4px;
}

.character-role {
  font-size: 12px;
  color: #667eea;
  margin-bottom: 8px;
  font-weight: 500;
}

.character-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.tag {
  font-size: 10px;
  background: #f0f4ff;
  color: #667eea;
  padding: 2px 8px;
  border-radius: 10px;
  font-weight: 500;
}

.character-actions {
  flex-shrink: 0;
  display: flex;
  align-items: center;
}

.favorite-btn {
  color: #6b7280;
  font-size: 16px;
  transition: all 0.2s ease;
}

.favorite-btn:hover {
  color: #fbbf24;
  transform: scale(1.1);
}

.favorite-btn.is-favorited {
  color: #fbbf24;
}
</style> 